<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="贷款编号" width="220">
        <template slot-scope="scope">
          {{ scope.row.applyNumber }}
        </template>
      </el-table-column>
      <el-table-column label="申请人姓名">
        <template slot-scope="scope">
          {{ scope.row.customerName }}
        </template>
      </el-table-column>
      <el-table-column label="贷款金额">
        <template slot-scope="scope">
          {{ scope.row.loanAmount }}
        </template>
      </el-table-column>
      <el-table-column label="贷款用途">
        <template slot-scope="scope">
          {{ scope.row.purpose }}
        </template>
      </el-table-column>
      <el-table-column label="还款期限">
        <template slot-scope="scope">
          {{ scope.row.loanTerm }}
        </template>
      </el-table-column>
      <el-table-column label="贷款利率">
        <template slot-scope="scope">
          {{ scope.row.loanRate }}%
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="审批状态" width="120" align="center">
        <template slot-scope="scope">
          {{ getStatusName(scope.row.status) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-button v-if="scope.row.status==1" type="text" @click="doCheck(scope.row.id,2)">业务员审批</el-button>
          <el-button v-if="scope.row.status==2" type="text" @click="doCheck(scope.row.id,3)">电话调查</el-button>
          <el-button v-if="scope.row.status==3" type="text" @click="doCheck(scope.row.id,4)">信贷审批</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="getTitle()"
      :visible.sync="confirmDialog"
      width="30%"
      center>
      <span>请确认是否审批通过!</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="confirmRefuse">拒 绝</el-button>
    <el-button type="primary" @click="doConfirm">通 过</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import { applyCheck, getList } from '@/api/loanApi'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      confirmDialog: false,
      id: null,
      status: null
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    getStatusName(status){
      if(status==1){return '客户提交申请'}
      if(status==2){return '业务员已审批'}
      if(status==3){return '电话调查通过'}
    },
    getTitle(){
      if(this.status==2){return '业务员审批'}
      if(this.status==3){return '电话调查'}
      if(this.status==4){return '信贷审批'}
    },
    fetchData() {
      this.listLoading = true
      getList().then(response => {
        this.list = response.data.records
        this.listLoading = false
      })
    },
    doApplyCheck(status) {
      applyCheck({ id: this.id, status }).then(response => {
        this.confirmDialog = false
        this.status = null
        this.id = null
        this.$message('操作成功!')
        this.fetchData()
      })
    },
    confirmRefuse() {
      this.doApplyCheck(this.status - 2)
    },
    doConfirm() {
      this.doApplyCheck(this.status)
    },
    doCheck(id, status) {
      this.confirmDialog = true
      this.id = id
      this.status = status
    }
  }
}
</script>
